<?php

namespace app\lib\exception;

use Exception;
use think\exception\Handle;
use think\exception\HttpException;

class ExceptionHandler extends Handle
{
    public $code;
    public $msg;
    public $errorCode;

    public function render(Exception $e)
    {
        
      if($e instanceof BaseException)
        {

            $this->code = $e->code;
            $this->msg = $e->msg;
            $this->errorCode = $e->errorCode;
        }else{
            // debug 开启时候显示默认的异常
            if(config('app.app_debug')) return parent::render($e);
            $this->code = 500;
            $this->msg = '服务器异常';
            $this->errorCode = 500;
        }
        return json(
            [
                'msg' =>  $this->msg,
                'errorCode' => $this->errorCode
            ],
            $this->code
        );
    }
}
